package top.shen33.yang.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import top.shen33.yang.base.BusinessException;
import top.shen33.yang.base.R;

import top.shen33.yang.model.entity.SysFieldDetail;
import top.shen33.yang.services.SysFieldDetailService;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 字段 前端控制器
 * </p>
 *
 * @author oreilly
 * @since 2018-09-17
 */
@RestController
@RequestMapping("/sysFieldDetail")
    public class SysFieldDetailController extends BaseController<SysFieldDetail> {


            @Autowired
            private SysFieldDetailService sysFieldDetailService;




            @Override
            public Object doAdd(@RequestBody SysFieldDetail sysFieldDetail) throws Exception {

                    sysFieldDetailService.doAdd(sysFieldDetail);
                    return R.ok(sysFieldDetail);
            }

            @Override
            public Object deDelete(String[] ids) throws Exception {

                    sysFieldDetailService.doDelete(ids);
                    return R.ok("delete ok");
            }

            @Override
            public Object doUpdate(@RequestBody SysFieldDetail sysFieldDetail) throws Exception {

                    sysFieldDetailService.doUpdate(sysFieldDetail);
                    return R.ok(sysFieldDetail);
            }

            @Override
            public Object getOne(@PathVariable String id) throws Exception {
                    SysFieldDetail sysFieldDetail = sysFieldDetailService.getOne(id);
                    return R.ok(sysFieldDetail);
            }


            public Object all(SysFieldDetail sysFieldDetail) throws Exception{
                    List<SysFieldDetail> sysFieldDetailList = sysFieldDetailService.all(sysFieldDetail);
                    return R.ok(sysFieldDetailList);
            }

//            @Override
//            public Object all(SysFieldDetail sysFieldDetail) throws Exception{
//                Page<SysFieldDetail> sysFieldDetailPage = sysFieldDetailService.pageInfo(null, sysFieldDetail);
//                List<SysFieldDetail> sysFieldDetailList = sysFieldDetailPage.getRecords();
//                return R.ok(sysFieldDetailList);
//            }

            @Override
            public Object pageInfo(Page page, SysFieldDetail sysFieldDetail) throws Exception {

                    Page<SysFieldDetail> sysFieldDetailPage = sysFieldDetailService.pageInfo(page, sysFieldDetail);
                    return R.ok("page",sysFieldDetailPage);
            }



            @Override
            public void exportExcel( HttpServletResponse response,SysFieldDetail sysFieldDetail) throws Exception {

                    Page<SysFieldDetail> sysFieldDetailPage = sysFieldDetailService.pageInfo(null, sysFieldDetail);
                    List<SysFieldDetail> dataList = sysFieldDetailPage.getRecords();

                    String fileName = "sysFieldDetail表.xls";
                    response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
                    Workbook exportExcel = ExcelExportUtil.exportExcel(new ExportParams(null, "ipProxy"), SysFieldDetail.class,
                    dataList);
                    exportExcel.write(response.getOutputStream());
            }

            @Override
            public Object importExcel(@RequestParam("file") MultipartFile file) throws Exception {

                    //		//判断文件是否为空
                    //		if(file==null) return null;
                    //		//获取文件名
                    //		//进一步判断文件是否为空（即判断其大小是否为0或其名称是否为null）
                    //		long size=file.getSize();
                    //		if(size==0) return null;

                    ImportParams params = new ImportParams();
                    params.setTitleRows(0);
                    params.setHeadRows(1);
                    params.setNeedSave(false);
                    List<SysFieldDetail> importDataList = null;

                    try {
                    //1.从excel中读取数据
                    importDataList = ExcelImportUtil.importExcel(file.getInputStream(), SysFieldDetail.class,
                    params);
                    } catch (Exception e) {
                    e.printStackTrace();
                    throw new BusinessException("上传文件内容格式错误");
                    }


                    if(importDataList.size() ==0){
                      return R.err("上传文件内容为空");
                    }


                    //2. 自己做业务处理
                    sysFieldDetailService.doImport(importDataList);
                    return R.ok("导入数据成功");
            }



}

